package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.Group;
import com.entity.GroupApply;
import com.entity.GroupUser;
import com.entity.User;
import com.mapper.GroupApplyMapper;
import com.mapper.GroupMapper;
import com.mapper.GroupUserMapper;
import com.mapper.UserMapper;
import com.service.GroupUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XTC
 * @since 2024-07-10
 */
@Service
public class GroupUserServiceImpl extends ServiceImpl<GroupUserMapper, GroupUser> implements GroupUserService {
    @Autowired
    private GroupUserMapper groupUserMapper;

    @Autowired
    private GroupApplyMapper groupApplyMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GroupMapper groupMapper;

    @Override
    public List<User> getAllUserOfGroup(Integer groupId) {
        List<User> userList = new ArrayList<>();
        QueryWrapper<GroupUser> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id", groupId);
        List<GroupUser> groupUsers = this.groupUserMapper.selectList(wrapper);
        for(GroupUser groupUser : groupUsers) {
            User user = this.userMapper.selectById(groupUser.getUserId());
            userList.add(user);
        }
        return userList;
    }

    @Override
    public boolean addMasterToGroup(Group group) {
        GroupUser groupUser = new GroupUser();
        groupUser.setGroupId(group.getId());
        groupUser.setUserId(group.getMasterId());
        groupUser.setStatus(1);
        return this.groupUserMapper.insert(groupUser) > 0;
    }

    @Override
    public boolean addUserToGroupByApplyId(Integer applyId) {
        QueryWrapper<GroupApply> wrapper = new QueryWrapper<>();
        wrapper.eq("id", applyId);
        GroupApply groupApply = this.groupApplyMapper.selectOne(wrapper);
        GroupUser groupUser = new GroupUser();
        BeanUtils.copyProperties(groupApply, groupUser);
        groupUser.setId(null);
        return this.groupUserMapper.insert(groupUser) > 0;
    }

    @Override
    public boolean addUsersToGroup(Integer groupId, List<Integer> userIdList) {
        for(Integer userId : userIdList) {
            GroupUser groupUser = new GroupUser();
            groupUser.setUserId(userId);
            groupUser.setGroupId(groupId);
            groupUser.setStatus(1);
            this.groupUserMapper.insert(groupUser);
        }
        return true;
    }

    private List<GroupUser> getGroupUserByUserId(Integer userId) {
        QueryWrapper<GroupUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return this.groupUserMapper.selectList(wrapper);
    }

    @Override
    public List<Group> getAllJoinedGroup(Integer userId) {
        List<GroupUser> groupUsers = this.getGroupUserByUserId(userId);
        List<Group> groupList = new ArrayList<>();
        for(GroupUser groupUser : groupUsers) {
            Group group = this.groupMapper.selectById(groupUser.getGroupId());
            groupList.add(group);
        }
        return groupList;
    }

    @Override
    public List<Group> getAllJoinedGroupByName(Integer userId, String name) {
        List<GroupUser> groupUserList = this.getGroupUserByUserId(userId);
        if(groupUserList.isEmpty()) {
            return new ArrayList<>();
        }
        List<Integer> idList = new ArrayList<>();
        for(GroupUser groupUser : groupUserList) {
            idList.add(groupUser.getGroupId());
        }
        QueryWrapper<Group> wrapper = new QueryWrapper<>();
        wrapper.in("id", idList)
        .like("name", name);
        return this.groupMapper.selectList(wrapper);
    }
}
